package com.module.retrofit.exception;

import android.net.ParseException;

import com.google.gson.JsonParseException;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;

import java.net.ConnectException;

import javax.net.ssl.SSLHandshakeException;

import retrofit2.HttpException;

import static com.module.retrofit.exception.ExceptionCode.BAD_GATEWAY;
import static com.module.retrofit.exception.ExceptionCode.FORBIDDEN;
import static com.module.retrofit.exception.ExceptionCode.GATEWAY_TIMEOUT;
import static com.module.retrofit.exception.ExceptionCode.HTTP_ERROR;
import static com.module.retrofit.exception.ExceptionCode.INTERNAL_SERVER_ERROR;
import static com.module.retrofit.exception.ExceptionCode.NET_ERROR;
import static com.module.retrofit.exception.ExceptionCode.NOT_FOUND;
import static com.module.retrofit.exception.ExceptionCode.PARSE_ERROR;
import static com.module.retrofit.exception.ExceptionCode.REQUEST_TIMEOUT;
import static com.module.retrofit.exception.ExceptionCode.SERVICE_UNAVAILABLE;
import static com.module.retrofit.exception.ExceptionCode.SSL_ERROR;
import static com.module.retrofit.exception.ExceptionCode.TIMEOUT_ERROR;
import static com.module.retrofit.exception.ExceptionCode.UNAUTHORIZED;
import static com.module.retrofit.exception.ExceptionCode.UNKNOWN_ERROR;

public class ExceptionHandler {

    public static ResponseException handle(Throwable e) {
        ResponseException responseException = null;
        if(e instanceof ResponseException){
            responseException = (ResponseException) e;
        } else if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            switch (httpException.code()) {
                case UNAUTHORIZED:
                case FORBIDDEN:
                case NOT_FOUND:
                case REQUEST_TIMEOUT:
                case GATEWAY_TIMEOUT:
                case INTERNAL_SERVER_ERROR:
                case BAD_GATEWAY:
                case SERVICE_UNAVAILABLE:
                default:
                    responseException = new ResponseException(HTTP_ERROR + ":" + httpException.code(), "网络连接错误");
                    break;
            }
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {
            responseException = new ResponseException(PARSE_ERROR, "解析错误");
        } else if (e instanceof ConnectException) {
            responseException = new ResponseException(NET_ERROR, "连接失败");
        } else if (e instanceof ConnectTimeoutException || e instanceof java.net.SocketTimeoutException) {
            responseException = new ResponseException(TIMEOUT_ERROR, "网络连接超时");
        } else if (e instanceof SSLHandshakeException) {
            responseException = new ResponseException(SSL_ERROR, "证书验证失败");
        } else {
            //有时候没有errorMessage
            responseException = new ResponseException(UNKNOWN_ERROR, e.toString());
        }
        return responseException;
    }
}
